package com.example.administrator.base.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.allenliu.versionchecklib.core.AllenChecker;
import com.allenliu.versionchecklib.core.VersionParams;
import com.example.administrator.base.baseActivity.BaseActivity;
import com.example.administrator.base.R;
import com.example.administrator.base.bean.ReLogout;
import com.example.administrator.base.service.DemoIntentService;
import com.example.administrator.base.service.Updata;
import com.example.administrator.base.util.GsonTools;
import com.example.administrator.base.util.IsOpenLocation;
import com.example.administrator.base.util.PermissionsChecker;
import com.example.administrator.base.util.SharedPreferencesUtils;
import com.igexin.sdk.PushManager;
import butterknife.ButterKnife;
import butterknife.InjectView;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class buffer extends BaseActivity       {

    @InjectView(R.id.textView)
    TextView textView;
    protected PermissionsChecker mPermissionsChecker; // 权限检测器
    private static final int REQUEST_CODE = 0; // 请求码
    // 所需的全部权限
    static final String[] PERMISSIONS = new String[]{
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA,
            Manifest.permission.CALL_PHONE

    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buffer);
        ButterKnife.inject(this);
        PushManager.getInstance().registerPushIntentService(this.getApplicationContext(), DemoIntentService.class);
        mPermissionsChecker = new PermissionsChecker(this);
        textView.setText(getVersion());

    }

    public String getVersion() {
        try {
            PackageManager manager = this.getPackageManager();
            PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
            String version = info.versionName;
            return this.getString(R.string.version_name) + version + "\n组员无删除版";
        } catch (Exception e) {
            e.printStackTrace();
            return this.getString(R.string.can_not_find_version_name);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (mPermissionsChecker.lacksPermissions(PERMISSIONS)) {
            startPermissionsActivity();
        }else {
            if (!IsOpenLocation.isOpenLocService(this)){
                showToast("请打开您的定位服务");
                IsOpenLocation.gotoLocServiceSettings(this);
            }else {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (!String.valueOf(SharedPreferencesUtils.getParam(buffer.this, "adminId", "")).equals("")) {
                            String id = String.valueOf(SharedPreferencesUtils.getParam(buffer.this,"adminId",""));
                            VersionParams.Builder builder = new VersionParams.Builder()
                                    .setRequestUrl("http://operate.99bicycle.com/OperateBicyclePro/administrator?action=GETOPERATEAPPVERSION&adminId="+id+"&type=0")
                                    .setCustomDownloadActivityClass(View.class)
                                    .setForceRedownload(false)
                                    .setService(Updata.class);
                            AllenChecker.startVersionCheck(buffer.this,builder.build());
                            stopService(new Intent(buffer.this, Updata.class));
                            startActivity(new Intent(buffer.this, MainActivity.class));
                            finish();
                        } else {
                            startActivity(new Intent(buffer.this, Login.class));
                            finish();
                        }
                    }
                }, 3000);
            }
        }
    }

    @Override
    protected void onResponseSuccess(Response response, String action) {

    }

    @Override
    protected void onResponseFailure(Throwable e, String action) {

    }

    private void startPermissionsActivity() {
        PermissionsActivity.startActivityForResult(this, REQUEST_CODE, PERMISSIONS);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (!String.valueOf(SharedPreferencesUtils.getParam(buffer.this, "adminId", "")).equals("")) {
                    startActivity(new Intent(buffer.this, MainActivity.class));
                    finish();
                } else {
                    startActivity(new Intent(buffer.this, Login.class));
                    finish();
                }
            }
        }, 3000);
    }

}
